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