wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 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 | d79de1d | 2013-07-08 09:37:19 +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 | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 17 | #include <malloc.h> |
Wolfgang Denk | 460a9ff | 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 | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Mike Frysinger | 78dcaf4 | 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 | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 25 | #elif defined(CONFIG_ENV_ADDR_REDUND) |
Igor Grinberg | 2182105 | 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 | 2182105 | 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 | 7e1cda6 | 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 | 2182105 | 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 | 23b54b9 | 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 | 7e1cda6 | 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 | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 43 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 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 | 460a9ff | 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 | 7e1cda6 | 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 | 460a9ff | 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 | 7e1cda6 | 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 | 7e1cda6 | 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 | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 60 | #ifdef CONFIG_ENV_ADDR_REDUND |
Igor Grinberg | 2182105 | 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 | f806271 | 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 | 2182105 | 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 | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 75 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 76 | if (crc1_ok && !crc2_ok) { |
| 77 | gd->env_addr = addr1; |
| 78 | gd->env_valid = 1; |
| 79 | } else if (!crc1_ok && crc2_ok) { |
| 80 | gd->env_addr = addr2; |
| 81 | gd->env_valid = 1; |
| 82 | } else if (!crc1_ok && !crc2_ok) { |
| 83 | gd->env_addr = addr_default; |
| 84 | gd->env_valid = 0; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 85 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 86 | gd->env_addr = addr1; |
| 87 | gd->env_valid = 1; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 88 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 89 | gd->env_addr = addr2; |
| 90 | gd->env_valid = 1; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 91 | } else if (flag1 == flag2) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 92 | gd->env_addr = addr1; |
| 93 | gd->env_valid = 2; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 94 | } else if (flag1 == 0xFF) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 95 | gd->env_addr = addr1; |
| 96 | gd->env_valid = 2; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 97 | } else if (flag2 == 0xFF) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 98 | gd->env_addr = addr2; |
| 99 | gd->env_valid = 2; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Wolfgang Denk | 460a9ff | 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 | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 108 | env_t env_new; |
| 109 | ssize_t len; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 110 | char *res, *saved_data = NULL; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 111 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 112 | int rc = 1; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 113 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 114 | ulong up_data = 0; |
wdenk | d730fbc | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 115 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 117 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 118 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 119 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 120 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 121 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 122 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | (ulong)flash_addr_new, end_addr_new); |
| 124 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 125 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 126 | goto done; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 127 | |
| 128 | res = (char *)&env_new.data; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 129 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 130 | if (len < 0) { |
| 131 | error("Cannot export environment: errno = %d\n", errno); |
| 132 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 133 | } |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 134 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 135 | env_new.flags = new_flag; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 136 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 137 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 138 | up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 139 | debug("Data to save 0x%lX\n", up_data); |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 140 | if (up_data) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 141 | saved_data = malloc(up_data); |
| 142 | if (saved_data == NULL) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 144 | up_data); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 145 | goto done; |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 146 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 147 | memcpy(saved_data, |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 148 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), |
| 149 | up_data); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 150 | debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n", |
| 151 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
| 152 | up_data, saved_data); |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 153 | } |
| 154 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 155 | puts("Erasing Flash..."); |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 156 | debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 157 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 158 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 159 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 160 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 161 | puts("Writing to Flash... "); |
| 162 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 163 | (ulong)&(flash_addr_new->data), |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 164 | sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data)); |
| 165 | rc = flash_write((char *)&env_new, (ulong)flash_addr_new, |
| 166 | sizeof(env_new)); |
| 167 | if (rc) |
| 168 | goto perror; |
| 169 | |
| 170 | rc = flash_write(&flag, (ulong)&(flash_addr->flags), |
| 171 | sizeof(flash_addr->flags)); |
| 172 | if (rc) |
| 173 | goto perror; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 174 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 175 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 176 | if (up_data) { /* restore the rest of sector */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 177 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
| 178 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 179 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 180 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 181 | up_data)) |
| 182 | goto perror; |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 183 | } |
| 184 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 185 | puts("done\n"); |
| 186 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 187 | { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 188 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 189 | ulong ltmp = end_addr; |
| 190 | |
| 191 | flash_addr = flash_addr_new; |
| 192 | flash_addr_new = etmp; |
| 193 | |
| 194 | end_addr = end_addr_new; |
| 195 | end_addr_new = ltmp; |
| 196 | } |
| 197 | |
| 198 | rc = 0; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 199 | goto done; |
| 200 | perror: |
| 201 | flash_perror(rc); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 202 | done: |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 203 | if (saved_data) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 204 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 205 | /* try to re-protect */ |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 206 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
| 207 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 208 | |
| 209 | return rc; |
| 210 | } |
| 211 | #endif /* CMD_SAVEENV */ |
| 212 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 213 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 215 | int env_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 216 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 217 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 218 | gd->env_addr = (ulong)&(env_ptr->data); |
| 219 | gd->env_valid = 1; |
| 220 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 221 | } |
Dirk Behme | afa1dd5 | 2007-08-20 07:09:05 +0200 | [diff] [blame] | 222 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 223 | gd->env_addr = (ulong)&default_environment[0]; |
| 224 | gd->env_valid = 0; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 225 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | #ifdef CMD_SAVEENV |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 229 | int saveenv(void) |
| 230 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 231 | env_t env_new; |
| 232 | ssize_t len; |
| 233 | int rc = 1; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 234 | char *res, *saved_data = NULL; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 235 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 236 | ulong up_data = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 237 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 238 | up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 239 | debug("Data to save 0x%lx\n", up_data); |
| 240 | if (up_data) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 241 | saved_data = malloc(up_data); |
| 242 | if (saved_data == NULL) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 243 | printf("Unable to save the rest of sector (%ld)\n", |
| 244 | up_data); |
| 245 | goto done; |
| 246 | } |
| 247 | memcpy(saved_data, |
| 248 | (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data); |
| 249 | debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n", |
| 250 | (ulong)flash_addr + CONFIG_ENV_SIZE, |
| 251 | up_data, |
| 252 | (ulong)saved_data); |
| 253 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 254 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 255 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 256 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 257 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 258 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
| 259 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 260 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 261 | res = (char *)&env_new.data; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 262 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 263 | if (len < 0) { |
| 264 | error("Cannot export environment: errno = %d\n", errno); |
| 265 | goto done; |
| 266 | } |
| 267 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 268 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 269 | puts("Erasing Flash..."); |
| 270 | if (flash_sect_erase((long)flash_addr, end_addr)) |
| 271 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 272 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 273 | puts("Writing to Flash... "); |
| 274 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 275 | if (rc != 0) |
| 276 | goto perror; |
| 277 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 278 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 279 | if (up_data) { /* restore the rest of sector */ |
| 280 | debug("Restoring the rest of data to 0x%lx len 0x%lx\n", |
| 281 | (ulong)flash_addr + CONFIG_ENV_SIZE, up_data); |
| 282 | if (flash_write(saved_data, |
| 283 | (long)flash_addr + CONFIG_ENV_SIZE, |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 284 | up_data)) |
| 285 | goto perror; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 286 | } |
| 287 | #endif |
| 288 | puts("done\n"); |
| 289 | rc = 0; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 290 | goto done; |
| 291 | perror: |
| 292 | flash_perror(rc); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 293 | done: |
| 294 | if (saved_data) |
| 295 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 296 | /* try to re-protect */ |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 297 | flash_sect_protect(1, (long)flash_addr, end_addr); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 298 | return rc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 299 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 300 | #endif /* CMD_SAVEENV */ |
| 301 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 302 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 303 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 304 | void env_relocate_spec(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 305 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 306 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 307 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 308 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 309 | ulong ltmp = end_addr; |
| 310 | |
| 311 | flash_addr = flash_addr_new; |
| 312 | flash_addr_new = etmp; |
| 313 | |
| 314 | end_addr = end_addr_new; |
| 315 | end_addr_new = ltmp; |
| 316 | } |
| 317 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 318 | if (flash_addr_new->flags != OBSOLETE_FLAG && |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 319 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) { |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 320 | char flag = OBSOLETE_FLAG; |
| 321 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 322 | gd->env_valid = 2; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 323 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 324 | flash_write(&flag, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 325 | (ulong)&(flash_addr_new->flags), |
| 326 | sizeof(flash_addr_new->flags)); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 327 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 328 | } |
| 329 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 330 | if (flash_addr->flags != ACTIVE_FLAG && |
| 331 | (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) { |
| 332 | char flag = ACTIVE_FLAG; |
| 333 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 334 | gd->env_valid = 2; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 335 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 336 | flash_write(&flag, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 337 | (ulong)&(flash_addr->flags), |
| 338 | sizeof(flash_addr->flags)); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 339 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (gd->env_valid == 2) |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 343 | puts("*** Warning - some problems detected " |
| 344 | "reading environment; recovered successfully\n\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 345 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 346 | |
| 347 | env_import((char *)flash_addr, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 348 | } |