Tom Rini | 10e4779 | 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 | 460a9ff | 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> |
Simon Glass | bf86488 | 2019-08-01 09:47:04 -0600 | [diff] [blame] | 14 | #include <env.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 | |
Simon Glass | c10a88e | 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 | 2182105 | 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 | 7e1cda6 | 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 | c10a88e | 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 | |
Rajesh Bhagat | 0e9fe16 | 2018-11-05 18:01:10 +0000 | [diff] [blame] | 48 | #if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \ |
| 49 | !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | #ifdef ENV_IS_EMBEDDED |
Simon Glass | 204eb22 | 2019-08-01 09:47:03 -0600 | [diff] [blame] | 51 | static env_t *env_ptr = &embedded_environment; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | #else /* ! ENV_IS_EMBEDDED */ |
| 53 | |
Rajesh Bhagat | 0e9fe16 | 2018-11-05 18:01:10 +0000 | [diff] [blame] | 54 | static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | #endif /* ENV_IS_EMBEDDED */ |
Rajesh Bhagat | 0e9fe16 | 2018-11-05 18:01:10 +0000 | [diff] [blame] | 56 | #endif |
| 57 | static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 59 | /* CONFIG_ENV_ADDR is supposed to be on sector boundary */ |
Simon Glass | 082af92 | 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 | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 62 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 63 | #ifdef CONFIG_ENV_ADDR_REDUND |
Simon Glass | 082af92 | 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 | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 67 | /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */ |
Simon Glass | 082af92 | 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 | 7e1cda6 | 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 | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 72 | #ifdef CONFIG_ENV_ADDR_REDUND |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 73 | #ifdef INITENV |
Simon Glass | 082af92 | 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 | f806271 | 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 | 2182105 | 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 | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 88 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 89 | if (crc1_ok && !crc2_ok) { |
| 90 | gd->env_addr = addr1; |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 91 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 92 | } else if (!crc1_ok && crc2_ok) { |
| 93 | gd->env_addr = addr2; |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 94 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 95 | } else if (!crc1_ok && !crc2_ok) { |
| 96 | gd->env_addr = addr_default; |
Simon Glass | 1f39d0b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 97 | gd->env_valid = ENV_INVALID; |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame^] | 98 | } else if (flag1 == ENV_REDUND_ACTIVE && |
| 99 | flag2 == ENV_REDUND_OBSOLETE) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 100 | gd->env_addr = addr1; |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 101 | gd->env_valid = ENV_VALID; |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame^] | 102 | } else if (flag1 == ENV_REDUND_OBSOLETE && |
| 103 | flag2 == ENV_REDUND_ACTIVE) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 104 | gd->env_addr = addr2; |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 105 | gd->env_valid = ENV_VALID; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 106 | } else if (flag1 == flag2) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 107 | gd->env_addr = addr1; |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 108 | gd->env_valid = ENV_REDUND; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 109 | } else if (flag1 == 0xFF) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 110 | gd->env_addr = addr1; |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 111 | gd->env_valid = ENV_REDUND; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 112 | } else if (flag2 == 0xFF) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 113 | gd->env_addr = addr2; |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 114 | gd->env_valid = ENV_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 117 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 118 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 119 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | |
| 121 | #ifdef CMD_SAVEENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 122 | static int env_flash_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 124 | env_t env_new; |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 125 | char *saved_data = NULL; |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame^] | 126 | char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 127 | int rc = 1; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 128 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 129 | ulong up_data = 0; |
wdenk | d730fbc | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 130 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 132 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
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 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 135 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 136 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 137 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 138 | (ulong)flash_addr_new, end_addr_new); |
| 139 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 140 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 141 | goto done; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 142 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 143 | rc = env_export(&env_new); |
| 144 | if (rc) |
| 145 | return rc; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 146 | env_new.flags = new_flag; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 148 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 149 | 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] | 150 | debug("Data to save 0x%lX\n", up_data); |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 151 | if (up_data) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 152 | saved_data = malloc(up_data); |
| 153 | if (saved_data == NULL) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 154 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 155 | up_data); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 156 | goto done; |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 157 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 158 | memcpy(saved_data, |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 159 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), |
| 160 | up_data); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 161 | debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n", |
| 162 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
| 163 | up_data, saved_data); |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 164 | } |
| 165 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 166 | puts("Erasing Flash..."); |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 167 | debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 168 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 169 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 170 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 171 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 172 | puts("Writing to Flash... "); |
| 173 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 174 | (ulong)&(flash_addr_new->data), |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 175 | sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data)); |
| 176 | rc = flash_write((char *)&env_new, (ulong)flash_addr_new, |
| 177 | sizeof(env_new)); |
| 178 | if (rc) |
| 179 | goto perror; |
| 180 | |
| 181 | rc = flash_write(&flag, (ulong)&(flash_addr->flags), |
| 182 | sizeof(flash_addr->flags)); |
| 183 | if (rc) |
| 184 | goto perror; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 185 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 186 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 187 | if (up_data) { /* restore the rest of sector */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 188 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
| 189 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 190 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 191 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 192 | up_data)) |
| 193 | goto perror; |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 194 | } |
| 195 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 196 | puts("done\n"); |
| 197 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 198 | { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 199 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 200 | ulong ltmp = end_addr; |
| 201 | |
| 202 | flash_addr = flash_addr_new; |
| 203 | flash_addr_new = etmp; |
| 204 | |
| 205 | end_addr = end_addr_new; |
| 206 | end_addr_new = ltmp; |
| 207 | } |
| 208 | |
| 209 | rc = 0; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 210 | goto done; |
| 211 | perror: |
| 212 | flash_perror(rc); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 213 | done: |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 214 | if (saved_data) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 215 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 216 | /* try to re-protect */ |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 217 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
| 218 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 219 | |
| 220 | return rc; |
| 221 | } |
| 222 | #endif /* CMD_SAVEENV */ |
| 223 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 224 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 225 | |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 226 | #ifdef INITENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 227 | static int env_flash_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 228 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 229 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 230 | gd->env_addr = (ulong)&(env_ptr->data); |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 231 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 232 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | } |
Dirk Behme | afa1dd5 | 2007-08-20 07:09:05 +0200 | [diff] [blame] | 234 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 235 | gd->env_addr = (ulong)&default_environment[0]; |
Simon Glass | 1f39d0b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 236 | gd->env_valid = ENV_INVALID; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 237 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 239 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 240 | |
| 241 | #ifdef CMD_SAVEENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 242 | static int env_flash_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 243 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 244 | env_t env_new; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 245 | int rc = 1; |
Tom Rini | cfbdf56 | 2014-04-17 16:20:39 -0400 | [diff] [blame] | 246 | char *saved_data = NULL; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 247 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 248 | ulong up_data = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 250 | up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 251 | debug("Data to save 0x%lx\n", up_data); |
| 252 | if (up_data) { |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 253 | saved_data = malloc(up_data); |
| 254 | if (saved_data == NULL) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 255 | printf("Unable to save the rest of sector (%ld)\n", |
| 256 | up_data); |
| 257 | goto done; |
| 258 | } |
| 259 | memcpy(saved_data, |
| 260 | (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data); |
| 261 | debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n", |
| 262 | (ulong)flash_addr + CONFIG_ENV_SIZE, |
| 263 | up_data, |
| 264 | (ulong)saved_data); |
| 265 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 266 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 267 | |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 268 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 269 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 270 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
| 271 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 272 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 273 | rc = env_export(&env_new); |
| 274 | if (rc) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 275 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 276 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 277 | puts("Erasing Flash..."); |
| 278 | if (flash_sect_erase((long)flash_addr, end_addr)) |
| 279 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 280 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 281 | puts("Writing to Flash... "); |
| 282 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 283 | if (rc != 0) |
| 284 | goto perror; |
| 285 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 286 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 287 | if (up_data) { /* restore the rest of sector */ |
| 288 | debug("Restoring the rest of data to 0x%lx len 0x%lx\n", |
| 289 | (ulong)flash_addr + CONFIG_ENV_SIZE, up_data); |
| 290 | if (flash_write(saved_data, |
| 291 | (long)flash_addr + CONFIG_ENV_SIZE, |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 292 | up_data)) |
| 293 | goto perror; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 294 | } |
| 295 | #endif |
| 296 | puts("done\n"); |
| 297 | rc = 0; |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 298 | goto done; |
| 299 | perror: |
| 300 | flash_perror(rc); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 301 | done: |
| 302 | if (saved_data) |
| 303 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 304 | /* try to re-protect */ |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 305 | flash_sect_protect(1, (long)flash_addr, end_addr); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 306 | return rc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 307 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 308 | #endif /* CMD_SAVEENV */ |
| 309 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 310 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 311 | |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 312 | #ifdef LOADENV |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 313 | static int env_flash_load(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 314 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 315 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 316 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 317 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 318 | ulong ltmp = end_addr; |
| 319 | |
| 320 | flash_addr = flash_addr_new; |
| 321 | flash_addr_new = etmp; |
| 322 | |
| 323 | end_addr = end_addr_new; |
| 324 | end_addr_new = ltmp; |
| 325 | } |
| 326 | |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame^] | 327 | if (flash_addr_new->flags != ENV_REDUND_OBSOLETE && |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 328 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) { |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame^] | 329 | char flag = ENV_REDUND_OBSOLETE; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 330 | |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 331 | gd->env_valid = ENV_REDUND; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 332 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 333 | flash_write(&flag, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 334 | (ulong)&(flash_addr_new->flags), |
| 335 | sizeof(flash_addr_new->flags)); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 336 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame^] | 339 | if (flash_addr->flags != ENV_REDUND_ACTIVE && |
| 340 | (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) { |
| 341 | char flag = ENV_REDUND_ACTIVE; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 342 | |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 343 | gd->env_valid = ENV_REDUND; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 344 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 345 | flash_write(&flag, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 346 | (ulong)&(flash_addr->flags), |
| 347 | sizeof(flash_addr->flags)); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 348 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 351 | if (gd->env_valid == ENV_REDUND) |
Igor Grinberg | 2182105 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 352 | puts("*** Warning - some problems detected " |
| 353 | "reading environment; recovered successfully\n\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 354 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 355 | |
Simon Goldschmidt | 35b4c32 | 2018-01-31 14:47:12 +0100 | [diff] [blame] | 356 | return env_import((char *)flash_addr, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 357 | } |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 358 | #endif /* LOADENV */ |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 359 | |
| 360 | U_BOOT_ENV_LOCATION(flash) = { |
| 361 | .location = ENVL_FLASH, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 362 | ENV_NAME("Flash") |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 363 | #ifdef LOADENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 364 | .load = env_flash_load, |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 365 | #endif |
| 366 | #ifdef CMD_SAVEENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 367 | .save = env_save_ptr(env_flash_save), |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 368 | #endif |
| 369 | #ifdef INITENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 370 | .init = env_flash_init, |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 371 | #endif |
| 372 | }; |