wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
| 7 | |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* #define DEBUG */ |
| 12 | |
| 13 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 14 | #include <command.h> |
| 15 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 16 | #include <linux/stddef.h> |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 17 | #include <malloc.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 18 | #include <search.h> |
| 19 | #include <errno.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 20 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 23 | #ifndef CONFIG_SPL_BUILD |
| 24 | # if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH) |
| 25 | # define CMD_SAVEENV |
| 26 | # elif defined(CONFIG_ENV_ADDR_REDUND) |
| 27 | # error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH |
| 28 | # endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 31 | #if defined(CONFIG_ENV_SIZE_REDUND) && \ |
| 32 | (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE) |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 33 | #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 36 | /* TODO(sjg@chromium.org): Figure out all these special cases */ |
| 37 | #if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \ |
| 38 | !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \ |
| 39 | !defined(CONFIG_TARGET_EDMINIV2)) || \ |
| 40 | !defined(CONFIG_SPL_BUILD) |
| 41 | #define LOADENV |
| 42 | #endif |
| 43 | |
| 44 | #if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD) |
| 45 | #define INITENV |
| 46 | #endif |
| 47 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 48 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 994bc67 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 49 | env_t *env_ptr = &environment; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 51 | static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | |
| 53 | #else /* ! ENV_IS_EMBEDDED */ |
| 54 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 55 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 56 | static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 57 | #endif /* ENV_IS_EMBEDDED */ |
| 58 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 59 | /* CONFIG_ENV_ADDR is supposed to be on sector boundary */ |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 60 | static ulong __maybe_unused end_addr = |
| 61 | CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 62 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 63 | #ifdef CONFIG_ENV_ADDR_REDUND |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 64 | |
| 65 | static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 66 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 67 | /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */ |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 68 | static ulong __maybe_unused end_addr_new = |
| 69 | CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 70 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 71 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 72 | #ifdef CONFIG_ENV_ADDR_REDUND |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 73 | #ifdef INITENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 74 | static int env_flash_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 75 | { |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 76 | int crc1_ok = 0, crc2_ok = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 77 | |
| 78 | uchar flag1 = flash_addr->flags; |
| 79 | uchar flag2 = flash_addr_new->flags; |
| 80 | |
| 81 | ulong addr_default = (ulong)&default_environment[0]; |
| 82 | ulong addr1 = (ulong)&(flash_addr->data); |
| 83 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 84 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 85 | crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc; |
| 86 | crc2_ok = |
| 87 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 88 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 89 | if (crc1_ok && !crc2_ok) { |
| 90 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 91 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 92 | } else if (!crc1_ok && crc2_ok) { |
| 93 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 94 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 95 | } else if (!crc1_ok && !crc2_ok) { |
| 96 | gd->env_addr = addr_default; |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 97 | gd->env_valid = ENV_INVALID; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 98 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 99 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 100 | gd->env_valid = ENV_VALID; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 101 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 102 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 103 | gd->env_valid = ENV_VALID; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 104 | } else if (flag1 == flag2) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 105 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 106 | gd->env_valid = ENV_REDUND; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 107 | } else if (flag1 == 0xFF) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 108 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 109 | gd->env_valid = ENV_REDUND; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 110 | } else if (flag2 == 0xFF) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 111 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 112 | gd->env_valid = ENV_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 115 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 117 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 118 | |
| 119 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 120 | static int env_flash_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 121 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 122 | env_t env_new; |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 123 | char *saved_data = NULL; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 124 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 125 | int rc = 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 126 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 127 | ulong up_data = 0; |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 128 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 130 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 132 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 133 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 134 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 135 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 136 | (ulong)flash_addr_new, end_addr_new); |
| 137 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 138 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 139 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 140 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 141 | rc = env_export(&env_new); |
| 142 | if (rc) |
| 143 | return rc; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 144 | env_new.flags = new_flag; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 145 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 146 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 147 | up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 148 | debug("Data to save 0x%lX\n", up_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 149 | if (up_data) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 150 | saved_data = malloc(up_data); |
| 151 | if (saved_data == NULL) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 152 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 153 | up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 154 | goto done; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 155 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 156 | memcpy(saved_data, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 157 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), |
| 158 | up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 159 | debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n", |
| 160 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
| 161 | up_data, saved_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 162 | } |
| 163 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 164 | puts("Erasing Flash..."); |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 165 | debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 166 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 167 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 168 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 169 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 170 | puts("Writing to Flash... "); |
| 171 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 172 | (ulong)&(flash_addr_new->data), |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 173 | sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data)); |
| 174 | rc = flash_write((char *)&env_new, (ulong)flash_addr_new, |
| 175 | sizeof(env_new)); |
| 176 | if (rc) |
| 177 | goto perror; |
| 178 | |
| 179 | rc = flash_write(&flag, (ulong)&(flash_addr->flags), |
| 180 | sizeof(flash_addr->flags)); |
| 181 | if (rc) |
| 182 | goto perror; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 183 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 184 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 185 | if (up_data) { /* restore the rest of sector */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 186 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
| 187 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 188 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 189 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 190 | up_data)) |
| 191 | goto perror; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 192 | } |
| 193 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 194 | puts("done\n"); |
| 195 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 196 | { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 197 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 198 | ulong ltmp = end_addr; |
| 199 | |
| 200 | flash_addr = flash_addr_new; |
| 201 | flash_addr_new = etmp; |
| 202 | |
| 203 | end_addr = end_addr_new; |
| 204 | end_addr_new = ltmp; |
| 205 | } |
| 206 | |
| 207 | rc = 0; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 208 | goto done; |
| 209 | perror: |
| 210 | flash_perror(rc); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 211 | done: |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 212 | if (saved_data) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 213 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | /* try to re-protect */ |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 215 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
| 216 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 217 | |
| 218 | return rc; |
| 219 | } |
| 220 | #endif /* CMD_SAVEENV */ |
| 221 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 222 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 223 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 224 | #ifdef INITENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 225 | static int env_flash_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 226 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 227 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 228 | gd->env_addr = (ulong)&(env_ptr->data); |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 229 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 230 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 231 | } |
Dirk Behme | 9bb8b20 | 2007-08-20 07:09:05 +0200 | [diff] [blame] | 232 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 233 | gd->env_addr = (ulong)&default_environment[0]; |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 234 | gd->env_valid = ENV_INVALID; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 235 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 237 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | |
| 239 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 240 | static int env_flash_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 242 | env_t env_new; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 243 | int rc = 1; |
Tom Rini | 3fa1981 | 2014-04-17 16:20:39 -0400 | [diff] [blame] | 244 | char *saved_data = NULL; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 245 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 246 | ulong up_data = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 247 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 248 | up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 249 | debug("Data to save 0x%lx\n", up_data); |
| 250 | if (up_data) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 251 | saved_data = malloc(up_data); |
| 252 | if (saved_data == NULL) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 253 | printf("Unable to save the rest of sector (%ld)\n", |
| 254 | up_data); |
| 255 | goto done; |
| 256 | } |
| 257 | memcpy(saved_data, |
| 258 | (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data); |
| 259 | debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n", |
| 260 | (ulong)flash_addr + CONFIG_ENV_SIZE, |
| 261 | up_data, |
| 262 | (ulong)saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 263 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 264 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 265 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 266 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 267 | |
| 268 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
| 269 | goto done; |
| 270 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 271 | rc = env_export(&env_new); |
| 272 | if (rc) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 273 | goto done; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 274 | |
| 275 | puts("Erasing Flash..."); |
| 276 | if (flash_sect_erase((long)flash_addr, end_addr)) |
| 277 | goto done; |
| 278 | |
| 279 | puts("Writing to Flash... "); |
| 280 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 281 | if (rc != 0) |
| 282 | goto perror; |
| 283 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 284 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 285 | if (up_data) { /* restore the rest of sector */ |
| 286 | debug("Restoring the rest of data to 0x%lx len 0x%lx\n", |
| 287 | (ulong)flash_addr + CONFIG_ENV_SIZE, up_data); |
| 288 | if (flash_write(saved_data, |
| 289 | (long)flash_addr + CONFIG_ENV_SIZE, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 290 | up_data)) |
| 291 | goto perror; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 292 | } |
| 293 | #endif |
| 294 | puts("done\n"); |
| 295 | rc = 0; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 296 | goto done; |
| 297 | perror: |
| 298 | flash_perror(rc); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 299 | done: |
| 300 | if (saved_data) |
| 301 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 302 | /* try to re-protect */ |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 303 | flash_sect_protect(1, (long)flash_addr, end_addr); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 304 | return rc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 305 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 306 | #endif /* CMD_SAVEENV */ |
| 307 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 308 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 309 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 310 | #ifdef LOADENV |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 311 | static int env_flash_load(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 312 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 313 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 314 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 315 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 316 | ulong ltmp = end_addr; |
| 317 | |
| 318 | flash_addr = flash_addr_new; |
| 319 | flash_addr_new = etmp; |
| 320 | |
| 321 | end_addr = end_addr_new; |
| 322 | end_addr_new = ltmp; |
| 323 | } |
| 324 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 325 | if (flash_addr_new->flags != OBSOLETE_FLAG && |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 326 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) { |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 327 | char flag = OBSOLETE_FLAG; |
| 328 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 329 | gd->env_valid = ENV_REDUND; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 330 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 331 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 332 | (ulong)&(flash_addr_new->flags), |
| 333 | sizeof(flash_addr_new->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 334 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 335 | } |
| 336 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 337 | if (flash_addr->flags != ACTIVE_FLAG && |
| 338 | (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) { |
| 339 | char flag = ACTIVE_FLAG; |
| 340 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 341 | gd->env_valid = ENV_REDUND; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 342 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 343 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 344 | (ulong)&(flash_addr->flags), |
| 345 | sizeof(flash_addr->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 346 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 349 | if (gd->env_valid == ENV_REDUND) |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 350 | puts("*** Warning - some problems detected " |
| 351 | "reading environment; recovered successfully\n\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 352 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 353 | |
Simon Goldschmidt | 2166ebf | 2018-01-31 14:47:12 +0100 | [diff] [blame^] | 354 | return env_import((char *)flash_addr, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 355 | } |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 356 | #endif /* LOADENV */ |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 357 | |
| 358 | U_BOOT_ENV_LOCATION(flash) = { |
| 359 | .location = ENVL_FLASH, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 360 | ENV_NAME("Flash") |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 361 | #ifdef LOADENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 362 | .load = env_flash_load, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 363 | #endif |
| 364 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 365 | .save = env_save_ptr(env_flash_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 366 | #endif |
| 367 | #ifdef INITENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 368 | .init = env_flash_init, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 369 | #endif |
| 370 | }; |