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 | |
Mike Frysinger | bdab39d | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 23 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | #define CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 25 | #elif defined(CONFIG_ENV_ADDR_REDUND) |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 26 | #error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 29 | #if defined(CONFIG_ENV_SIZE_REDUND) && \ |
| 30 | (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE) |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 31 | #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 34 | char *env_name_spec = "Flash"; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 35 | |
| 36 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 994bc67 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 37 | env_t *env_ptr = &environment; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 39 | static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 40 | |
| 41 | #else /* ! ENV_IS_EMBEDDED */ |
| 42 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 43 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 44 | static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | #endif /* ENV_IS_EMBEDDED */ |
| 46 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 47 | #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND) |
| 48 | /* CONFIG_ENV_ADDR is supposed to be on sector boundary */ |
| 49 | static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1; |
| 50 | #endif |
| 51 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_ENV_ADDR_REDUND |
| 53 | static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 55 | /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */ |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 56 | static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 57 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 59 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 60 | #ifdef CONFIG_ENV_ADDR_REDUND |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 61 | int env_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | { |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 63 | int crc1_ok = 0, crc2_ok = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 64 | |
| 65 | uchar flag1 = flash_addr->flags; |
| 66 | uchar flag2 = flash_addr_new->flags; |
| 67 | |
| 68 | ulong addr_default = (ulong)&default_environment[0]; |
| 69 | ulong addr1 = (ulong)&(flash_addr->data); |
| 70 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 71 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 72 | crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc; |
| 73 | crc2_ok = |
| 74 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 75 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 76 | if (crc1_ok && !crc2_ok) { |
| 77 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 78 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 79 | } else if (!crc1_ok && crc2_ok) { |
| 80 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 81 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 82 | } else if (!crc1_ok && !crc2_ok) { |
| 83 | gd->env_addr = addr_default; |
| 84 | gd->env_valid = 0; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 85 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 86 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 87 | gd->env_valid = ENV_VALID; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 88 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 89 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 90 | gd->env_valid = ENV_VALID; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 91 | } else if (flag1 == flag2) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 92 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 93 | gd->env_valid = ENV_REDUND; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 94 | } else if (flag1 == 0xFF) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 95 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 96 | gd->env_valid = ENV_REDUND; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 97 | } else if (flag2 == 0xFF) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 98 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 99 | gd->env_valid = ENV_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 102 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #ifdef CMD_SAVEENV |
| 106 | int saveenv(void) |
| 107 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 108 | env_t env_new; |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 109 | char *saved_data = NULL; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 110 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 111 | int rc = 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 112 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 113 | ulong up_data = 0; |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 114 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 115 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 116 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 117 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 118 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 119 | goto done; |
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 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 122 | (ulong)flash_addr_new, end_addr_new); |
| 123 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 124 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 125 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 127 | rc = env_export(&env_new); |
| 128 | if (rc) |
| 129 | return rc; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 130 | env_new.flags = new_flag; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 131 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 132 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 133 | 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] | 134 | debug("Data to save 0x%lX\n", up_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 135 | if (up_data) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 136 | saved_data = malloc(up_data); |
| 137 | if (saved_data == NULL) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 138 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 139 | up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 140 | goto done; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 141 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 142 | memcpy(saved_data, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 143 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), |
| 144 | up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 145 | debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n", |
| 146 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
| 147 | up_data, saved_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 148 | } |
| 149 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 150 | puts("Erasing Flash..."); |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 151 | debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 152 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 153 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 154 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 156 | puts("Writing to Flash... "); |
| 157 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 158 | (ulong)&(flash_addr_new->data), |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 159 | sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data)); |
| 160 | rc = flash_write((char *)&env_new, (ulong)flash_addr_new, |
| 161 | sizeof(env_new)); |
| 162 | if (rc) |
| 163 | goto perror; |
| 164 | |
| 165 | rc = flash_write(&flag, (ulong)&(flash_addr->flags), |
| 166 | sizeof(flash_addr->flags)); |
| 167 | if (rc) |
| 168 | goto perror; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 169 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 170 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 171 | if (up_data) { /* restore the rest of sector */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 172 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
| 173 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 174 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 175 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 176 | up_data)) |
| 177 | goto perror; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 178 | } |
| 179 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 180 | puts("done\n"); |
| 181 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 182 | { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 183 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 184 | ulong ltmp = end_addr; |
| 185 | |
| 186 | flash_addr = flash_addr_new; |
| 187 | flash_addr_new = etmp; |
| 188 | |
| 189 | end_addr = end_addr_new; |
| 190 | end_addr_new = ltmp; |
| 191 | } |
| 192 | |
| 193 | rc = 0; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 194 | goto done; |
| 195 | perror: |
| 196 | flash_perror(rc); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 197 | done: |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 198 | if (saved_data) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 199 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 200 | /* try to re-protect */ |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 201 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
| 202 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 203 | |
| 204 | return rc; |
| 205 | } |
| 206 | #endif /* CMD_SAVEENV */ |
| 207 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 208 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 209 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 210 | int env_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 211 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 212 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 213 | gd->env_addr = (ulong)&(env_ptr->data); |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 214 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 215 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 216 | } |
Dirk Behme | 9bb8b20 | 2007-08-20 07:09:05 +0200 | [diff] [blame] | 217 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 218 | gd->env_addr = (ulong)&default_environment[0]; |
| 219 | gd->env_valid = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 220 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | #ifdef CMD_SAVEENV |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 224 | int saveenv(void) |
| 225 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 226 | env_t env_new; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 227 | int rc = 1; |
Tom Rini | 3fa1981 | 2014-04-17 16:20:39 -0400 | [diff] [blame] | 228 | char *saved_data = NULL; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 229 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 230 | ulong up_data = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 231 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 232 | up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 233 | debug("Data to save 0x%lx\n", up_data); |
| 234 | if (up_data) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 235 | saved_data = malloc(up_data); |
| 236 | if (saved_data == NULL) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 237 | printf("Unable to save the rest of sector (%ld)\n", |
| 238 | up_data); |
| 239 | goto done; |
| 240 | } |
| 241 | memcpy(saved_data, |
| 242 | (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data); |
| 243 | debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n", |
| 244 | (ulong)flash_addr + CONFIG_ENV_SIZE, |
| 245 | up_data, |
| 246 | (ulong)saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 247 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 248 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 250 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 251 | |
| 252 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
| 253 | goto done; |
| 254 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 255 | rc = env_export(&env_new); |
| 256 | if (rc) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 257 | goto done; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 258 | |
| 259 | puts("Erasing Flash..."); |
| 260 | if (flash_sect_erase((long)flash_addr, end_addr)) |
| 261 | goto done; |
| 262 | |
| 263 | puts("Writing to Flash... "); |
| 264 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 265 | if (rc != 0) |
| 266 | goto perror; |
| 267 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 268 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 269 | if (up_data) { /* restore the rest of sector */ |
| 270 | debug("Restoring the rest of data to 0x%lx len 0x%lx\n", |
| 271 | (ulong)flash_addr + CONFIG_ENV_SIZE, up_data); |
| 272 | if (flash_write(saved_data, |
| 273 | (long)flash_addr + CONFIG_ENV_SIZE, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 274 | up_data)) |
| 275 | goto perror; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 276 | } |
| 277 | #endif |
| 278 | puts("done\n"); |
| 279 | rc = 0; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 280 | goto done; |
| 281 | perror: |
| 282 | flash_perror(rc); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 283 | done: |
| 284 | if (saved_data) |
| 285 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 286 | /* try to re-protect */ |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 287 | flash_sect_protect(1, (long)flash_addr, end_addr); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 288 | return rc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 289 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 290 | #endif /* CMD_SAVEENV */ |
| 291 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 292 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 293 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 294 | void env_relocate_spec(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 295 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 296 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 297 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 298 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 299 | ulong ltmp = end_addr; |
| 300 | |
| 301 | flash_addr = flash_addr_new; |
| 302 | flash_addr_new = etmp; |
| 303 | |
| 304 | end_addr = end_addr_new; |
| 305 | end_addr_new = ltmp; |
| 306 | } |
| 307 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 308 | if (flash_addr_new->flags != OBSOLETE_FLAG && |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 309 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) { |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 310 | char flag = OBSOLETE_FLAG; |
| 311 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 312 | gd->env_valid = ENV_REDUND; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 313 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 314 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 315 | (ulong)&(flash_addr_new->flags), |
| 316 | sizeof(flash_addr_new->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 317 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 318 | } |
| 319 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 320 | if (flash_addr->flags != ACTIVE_FLAG && |
| 321 | (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) { |
| 322 | char flag = ACTIVE_FLAG; |
| 323 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 324 | gd->env_valid = ENV_REDUND; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 325 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 326 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 327 | (ulong)&(flash_addr->flags), |
| 328 | sizeof(flash_addr->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 329 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame^] | 332 | if (gd->env_valid == ENV_REDUND) |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 333 | puts("*** Warning - some problems detected " |
| 334 | "reading environment; recovered successfully\n\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 335 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 336 | |
| 337 | env_import((char *)flash_addr, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 338 | } |