]> Git Repo - J-u-boot.git/blame - env/flash.c
env: mmc: Add missing eMMC bootpart restoration to env erase
[J-u-boot.git] / env / flash.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
c609719b 2/*
ea882baf 3 * (C) Copyright 2000-2010
c609719b
WD
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <[email protected]>
c609719b
WD
8 */
9
10/* #define DEBUG */
11
12#include <common.h>
c609719b 13#include <command.h>
db197010 14#include <env.h>
f3998fdc 15#include <env_internal.h>
0ee48252 16#include <flash.h>
f7ae49fc 17#include <log.h>
401d1c4f 18#include <asm/global_data.h>
c609719b 19#include <linux/stddef.h>
47cd00fa 20#include <malloc.h>
ea882baf
WD
21#include <search.h>
22#include <errno.h>
3db71108 23#include <u-boot/crc.h>
c609719b 24
d87080b7
WD
25DECLARE_GLOBAL_DATA_PTR;
26
4415f1d1
SG
27#ifndef CONFIG_SPL_BUILD
28# if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
29# define CMD_SAVEENV
30# elif defined(CONFIG_ENV_ADDR_REDUND)
31# error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
32# endif
c609719b
WD
33#endif
34
4415f1d1
SG
35/* TODO([email protected]): Figure out all these special cases */
36#if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
37 !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
38 !defined(CONFIG_TARGET_EDMINIV2)) || \
39 !defined(CONFIG_SPL_BUILD)
40#define LOADENV
41#endif
42
43#if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
44#define INITENV
45#endif
46
b6cba297
RB
47#if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
48 !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
c609719b 49#ifdef ENV_IS_EMBEDDED
be54ec16 50static env_t *env_ptr = &embedded_environment;
c609719b
WD
51#else /* ! ENV_IS_EMBEDDED */
52
b6cba297 53static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
c609719b 54#endif /* ENV_IS_EMBEDDED */
b6cba297
RB
55#endif
56static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
c609719b 57
ea882baf 58/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
e5bce247
SG
59static ulong __maybe_unused end_addr =
60 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
ea882baf 61
0e8d1586 62#ifdef CONFIG_ENV_ADDR_REDUND
e5bce247
SG
63
64static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
c609719b 65
ea882baf 66/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
e5bce247
SG
67static ulong __maybe_unused end_addr_new =
68 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
0e8d1586 69#endif /* CONFIG_ENV_ADDR_REDUND */
c609719b 70
0e8d1586 71#ifdef CONFIG_ENV_ADDR_REDUND
4415f1d1 72#ifdef INITENV
e5bce247 73static int env_flash_init(void)
c609719b 74{
8ed96046 75 int crc1_ok = 0, crc2_ok = 0;
c609719b
WD
76
77 uchar flag1 = flash_addr->flags;
78 uchar flag2 = flash_addr_new->flags;
79
80 ulong addr_default = (ulong)&default_environment[0];
81 ulong addr1 = (ulong)&(flash_addr->data);
82 ulong addr2 = (ulong)&(flash_addr_new->data);
83
82b54b97
IG
84 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
85 crc2_ok =
86 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
87
88 if (crc1_ok && !crc2_ok) {
89 gd->env_addr = addr1;
203e94f6 90 gd->env_valid = ENV_VALID;
82b54b97
IG
91 } else if (!crc1_ok && crc2_ok) {
92 gd->env_addr = addr2;
203e94f6 93 gd->env_valid = ENV_VALID;
82b54b97
IG
94 } else if (!crc1_ok && !crc2_ok) {
95 gd->env_addr = addr_default;
2d7cb5b4 96 gd->env_valid = ENV_INVALID;
d3716dd6
SG
97 } else if (flag1 == ENV_REDUND_ACTIVE &&
98 flag2 == ENV_REDUND_OBSOLETE) {
82b54b97 99 gd->env_addr = addr1;
203e94f6 100 gd->env_valid = ENV_VALID;
d3716dd6
SG
101 } else if (flag1 == ENV_REDUND_OBSOLETE &&
102 flag2 == ENV_REDUND_ACTIVE) {
82b54b97 103 gd->env_addr = addr2;
203e94f6 104 gd->env_valid = ENV_VALID;
c3f9d493 105 } else if (flag1 == flag2) {
82b54b97 106 gd->env_addr = addr1;
203e94f6 107 gd->env_valid = ENV_REDUND;
c3f9d493 108 } else if (flag1 == 0xFF) {
82b54b97 109 gd->env_addr = addr1;
203e94f6 110 gd->env_valid = ENV_REDUND;
c3f9d493 111 } else if (flag2 == 0xFF) {
82b54b97 112 gd->env_addr = addr2;
203e94f6 113 gd->env_valid = ENV_REDUND;
c609719b
WD
114 }
115
ea882baf 116 return 0;
c609719b 117}
4415f1d1 118#endif
c609719b
WD
119
120#ifdef CMD_SAVEENV
e5bce247 121static int env_flash_save(void)
c609719b 122{
ea882baf 123 env_t env_new;
7ce1526e 124 char *saved_data = NULL;
d3716dd6 125 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
82b54b97 126 int rc = 1;
0e8d1586 127#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
ea882baf 128 ulong up_data = 0;
500545cc 129#endif
c609719b 130
82b54b97 131 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
c609719b 132
82b54b97 133 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
ea882baf 134 goto done;
c609719b 135
ea882baf 136 debug("Protect off %08lX ... %08lX\n",
c609719b
WD
137 (ulong)flash_addr_new, end_addr_new);
138
82b54b97 139 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
ea882baf 140 goto done;
ea882baf 141
7ce1526e
MV
142 rc = env_export(&env_new);
143 if (rc)
144 return rc;
82b54b97 145 env_new.flags = new_flag;
c609719b 146
0e8d1586 147#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
82b54b97 148 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
ea882baf 149 debug("Data to save 0x%lX\n", up_data);
47cd00fa 150 if (up_data) {
82b54b97
IG
151 saved_data = malloc(up_data);
152 if (saved_data == NULL) {
8bde7f77 153 printf("Unable to save the rest of sector (%ld)\n",
47cd00fa 154 up_data);
ea882baf 155 goto done;
47cd00fa 156 }
8bde7f77 157 memcpy(saved_data,
82b54b97
IG
158 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
159 up_data);
ea882baf
WD
160 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
161 (long)flash_addr_new + CONFIG_ENV_SIZE,
162 up_data, saved_data);
47cd00fa
WD
163 }
164#endif
ea882baf 165 puts("Erasing Flash...");
82b54b97 166 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
c609719b 167
82b54b97 168 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
ea882baf 169 goto done;
c609719b 170
ea882baf
WD
171 puts("Writing to Flash... ");
172 debug(" %08lX ... %08lX ...",
c609719b 173 (ulong)&(flash_addr_new->data),
82b54b97
IG
174 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
175 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
176 sizeof(env_new));
177 if (rc)
178 goto perror;
179
180 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
181 sizeof(flash_addr->flags));
182 if (rc)
183 goto perror;
c609719b 184
0e8d1586 185#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
47cd00fa 186 if (up_data) { /* restore the rest of sector */
ea882baf
WD
187 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
188 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
8bde7f77 189 if (flash_write(saved_data,
0e8d1586 190 (long)flash_addr_new + CONFIG_ENV_SIZE,
82b54b97
IG
191 up_data))
192 goto perror;
47cd00fa
WD
193 }
194#endif
ea882baf
WD
195 puts("done\n");
196
c609719b 197 {
82b54b97 198 env_t *etmp = flash_addr;
c609719b
WD
199 ulong ltmp = end_addr;
200
201 flash_addr = flash_addr_new;
202 flash_addr_new = etmp;
203
204 end_addr = end_addr_new;
205 end_addr_new = ltmp;
206 }
207
208 rc = 0;
82b54b97
IG
209 goto done;
210perror:
211 flash_perror(rc);
ea882baf 212done:
47cd00fa 213 if (saved_data)
ea882baf 214 free(saved_data);
c609719b 215 /* try to re-protect */
82b54b97
IG
216 flash_sect_protect(1, (ulong)flash_addr, end_addr);
217 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
c609719b
WD
218
219 return rc;
220}
221#endif /* CMD_SAVEENV */
222
0e8d1586 223#else /* ! CONFIG_ENV_ADDR_REDUND */
c609719b 224
4415f1d1 225#ifdef INITENV
e5bce247 226static int env_flash_init(void)
c609719b 227{
c609719b 228 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
82b54b97 229 gd->env_addr = (ulong)&(env_ptr->data);
203e94f6 230 gd->env_valid = ENV_VALID;
82b54b97 231 return 0;
c609719b 232 }
9bb8b209 233
82b54b97 234 gd->env_addr = (ulong)&default_environment[0];
2d7cb5b4 235 gd->env_valid = ENV_INVALID;
ea882baf 236 return 0;
c609719b 237}
4415f1d1 238#endif
c609719b
WD
239
240#ifdef CMD_SAVEENV
e5bce247 241static int env_flash_save(void)
c609719b 242{
ea882baf 243 env_t env_new;
ea882baf 244 int rc = 1;
3fa1981e 245 char *saved_data = NULL;
ea882baf
WD
246#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
247 ulong up_data = 0;
c609719b 248
82b54b97 249 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
ea882baf
WD
250 debug("Data to save 0x%lx\n", up_data);
251 if (up_data) {
82b54b97
IG
252 saved_data = malloc(up_data);
253 if (saved_data == NULL) {
ea882baf
WD
254 printf("Unable to save the rest of sector (%ld)\n",
255 up_data);
256 goto done;
257 }
258 memcpy(saved_data,
259 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
260 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
261 (ulong)flash_addr + CONFIG_ENV_SIZE,
262 up_data,
263 (ulong)saved_data);
264 }
0e8d1586 265#endif /* CONFIG_ENV_SECT_SIZE */
c609719b 266
82b54b97 267 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
c609719b 268
ea882baf
WD
269 if (flash_sect_protect(0, (long)flash_addr, end_addr))
270 goto done;
c609719b 271
7ce1526e
MV
272 rc = env_export(&env_new);
273 if (rc)
ea882baf 274 goto done;
c609719b 275
ea882baf
WD
276 puts("Erasing Flash...");
277 if (flash_sect_erase((long)flash_addr, end_addr))
278 goto done;
c609719b 279
ea882baf
WD
280 puts("Writing to Flash... ");
281 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
82b54b97
IG
282 if (rc != 0)
283 goto perror;
284
ea882baf
WD
285#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
286 if (up_data) { /* restore the rest of sector */
287 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
288 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
289 if (flash_write(saved_data,
290 (long)flash_addr + CONFIG_ENV_SIZE,
82b54b97
IG
291 up_data))
292 goto perror;
ea882baf
WD
293 }
294#endif
295 puts("done\n");
296 rc = 0;
82b54b97
IG
297 goto done;
298perror:
299 flash_perror(rc);
ea882baf
WD
300done:
301 if (saved_data)
302 free(saved_data);
c609719b 303 /* try to re-protect */
82b54b97 304 flash_sect_protect(1, (long)flash_addr, end_addr);
ea882baf 305 return rc;
c609719b 306}
c609719b
WD
307#endif /* CMD_SAVEENV */
308
0e8d1586 309#endif /* CONFIG_ENV_ADDR_REDUND */
c609719b 310
e5bce247 311#ifdef LOADENV
c5951991 312static int env_flash_load(void)
c609719b 313{
0e8d1586 314#ifdef CONFIG_ENV_ADDR_REDUND
c3f9d493 315 if (gd->env_addr != (ulong)&(flash_addr->data)) {
ea882baf 316 env_t *etmp = flash_addr;
c609719b
WD
317 ulong ltmp = end_addr;
318
319 flash_addr = flash_addr_new;
320 flash_addr_new = etmp;
321
322 end_addr = end_addr_new;
323 end_addr_new = ltmp;
324 }
325
d3716dd6 326 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
82b54b97 327 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
d3716dd6 328 char flag = ENV_REDUND_OBSOLETE;
c3f9d493 329
203e94f6 330 gd->env_valid = ENV_REDUND;
ea882baf 331 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
c3f9d493 332 flash_write(&flag,
8bde7f77
WD
333 (ulong)&(flash_addr_new->flags),
334 sizeof(flash_addr_new->flags));
ea882baf 335 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
c609719b
WD
336 }
337
d3716dd6
SG
338 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
339 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
340 char flag = ENV_REDUND_ACTIVE;
c3f9d493 341
203e94f6 342 gd->env_valid = ENV_REDUND;
ea882baf 343 flash_sect_protect(0, (ulong)flash_addr, end_addr);
c3f9d493 344 flash_write(&flag,
8bde7f77
WD
345 (ulong)&(flash_addr->flags),
346 sizeof(flash_addr->flags));
ea882baf 347 flash_sect_protect(1, (ulong)flash_addr, end_addr);
c609719b
WD
348 }
349
203e94f6 350 if (gd->env_valid == ENV_REDUND)
82b54b97
IG
351 puts("*** Warning - some problems detected "
352 "reading environment; recovered successfully\n\n");
0e8d1586 353#endif /* CONFIG_ENV_ADDR_REDUND */
ea882baf 354
890feeca 355 return env_import((char *)flash_addr, 1, H_EXTERNAL);
c609719b 356}
e5bce247 357#endif /* LOADENV */
4415f1d1
SG
358
359U_BOOT_ENV_LOCATION(flash) = {
360 .location = ENVL_FLASH,
ac358beb 361 ENV_NAME("Flash")
4415f1d1 362#ifdef LOADENV
e5bce247 363 .load = env_flash_load,
4415f1d1
SG
364#endif
365#ifdef CMD_SAVEENV
e5bce247 366 .save = env_save_ptr(env_flash_save),
4415f1d1
SG
367#endif
368#ifdef INITENV
e5bce247 369 .init = env_flash_init,
4415f1d1
SG
370#endif
371};
This page took 0.598334 seconds and 4 git commands to generate.